<?php
App::uses('AppController', 'Controller');

/**
 * Albums Controller
 *
 * @property Album $Album
 */
class AlbumsController extends AppController {

	public function beforeFilter() {
		parent::beforeFilter();
	}
	
	public function admin_home() {	
	}
	public function admin_index() {
	}
	public function admin_view() {	
	}
	
	public function admin_edit($id = null) {
		$this->Album->id = $id;
		if (!$this->Album->exists()) {
			throw new NotFoundException('Invalid album');
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Album->save($this->request->data)) {
				$this->Session->setFlash('The album has been saved');
				$url = $this->Session->read('Controller.toUrl');
				$this->redirect($url);
			} else {
				$this->Session->setFlash('The album could not be saved. Please, try again.');
			}
		} else {
			$options = array('conditions' => array('Album.' . $this->Album->primaryKey => $id));
			$this->request->data = $this->Album->find('first', $options);
		}
	}	

	public function ajax_index(){
		$this->paginate = array(
			'recursive' => -1,
			'limit' => 10,
			'order' => array('Album.name' => 'ASC')
		);
		$this->set('albums', $this->paginate());
	}
	
	public function api_add() {
		$error = -1;
		if ($this->request->is('post')) {
			$this->Album->create();
			if ($this->Album->save($this->request->data)) {
				$error = 0;
				$message = 'The album has been saved';
			} else {
				$error = 1;
				$message = 'The album could not be saved. Please, try again.';
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_delete($id = null) {
		$error = -1;
		$this->Album->id = $id;
		if (!$this->Album->exists()) {
			$message = 'Invalid album';
		}
		if ($this->Album->delete()) {
			$error = 0;
			$message = 'Album deleted';
		}
		else{
			$error = 1;
			$message = 'Album was not deleted';			
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_status($id = null){
		$error = -1;
		$this->Album->id = $id;
		if (!$this->Album->exists()) {
			$message = 'Invalid album';
		}
		else{		
			if($this->Album->updateAll(
			array('Album.status' => '1 - Album.status', 'modified' => 'NOW()'),
			array('Album.id' => $id))){
				$error = 0;
				$message = 'Status has been changed';
			}
			else{
				$error = 1;
				$message = 'Status could not be changed';					
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);		
	}
}
?>